package com.hwtx.form.util;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public interface UdfHandler {

    interface UdfFilter {

    }

    interface UdfProject {

    }

    interface UdfSort {

    }

    @Getter
    @SuperBuilder
    class BaseUdf {
        private List<QueryParserResult.Item> parameters;
        private Map<String, Object> params;
    }

    interface RouterUdf {
        String[] getRouterValue(Map<String, Object> params, List<QueryParserResult.Item> items);
    }


    default void handleFilter(UdfFilter udfFilter) {
    }

    default void handleProject(UdfProject udfProject) {

    }

    default void handleSort(UdfSort udfSort) {
    }


    boolean allowParameterVariable(List<QueryParserResult.Item> parameters);

    String getName();

    static UdfHandler getUdfHandler(String name) {
        List<UdfHandler> udfHandler = ResourceLoader.load(UdfHandler.class);
        List<UdfHandler> target = udfHandler.stream().filter(udf -> udf.getName().equalsIgnoreCase(name)).collect(Collectors.toList());
        if (target.isEmpty()) {
            throw Exceptions.DS_CONFIG_ERROR("无法获取自定义函数处理器，name = " + name);
        }
        if (target.size() > 1) {
            throw Exceptions.DS_CONFIG_ERROR("存在多个相同自定义函数处理器，name = " + name);
        }
        return target.getFirst();
    }

}
